﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using TodoMVC.BusinessServices;
using TodoMVC.Models;
using TodoMVC.Models.BusinessModels;
using TodoMVC.Models.DBModels;
using TodoMVC.Tools;

namespace TodoMVC.Main.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class TodoMVCController : ControllerBase
    {
        public TodoMVCBusinessServices _todoMVCBusinessServices { get; set; }

        public TodoMVCController(TodoMVCBusinessServices todoMVCBusinessServices)
        {
            _todoMVCBusinessServices = todoMVCBusinessServices;
        }

        [HttpGet("GetDateTimeNow")]
        public async Task<ActionResult<string>> GetDateTimeNow()
        {
            var nowStr = DateTimeHelper.GetDateTimeNowString();
            return await Task.FromResult(nowStr);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult<List<TodoItemResult>>> GetTodoItems()
        {
            var list1 = await _todoMVCBusinessServices.GetTodoItemsAsync();
            var list2 = list1.Select(t => t.ToTodoItemResult()).ToList();
            return list2;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<ActionResult<TodoItemResult>> GetTodoItem(long id)
        {
            var todoItem = await _todoMVCBusinessServices.GetTodoItemAsync(id);
            if (todoItem == null)
            {
                return NotFound();
            }
            return todoItem.ToTodoItemResult();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetByGuid/{guid}")]
        public async Task<ActionResult<TodoItemResult>> GetTodoItemByGuid(string guid)
        {
            var guid1 = new Guid(guid);
            var todoItem = await _todoMVCBusinessServices.GetTodoItemAsync(guid1);
            if (todoItem == null)
            {
                return NotFound();
            }
            return todoItem.ToTodoItemResult();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpPut("{id}")]
        public async Task<ActionResult<bool>> PutTodoItem(long id, TodoItem todoItem)
        {
            if (id != todoItem.Id)
            {
                return BadRequest();
            }

            if (!await _todoMVCBusinessServices.ExistTodoItemAsync(id))
            {
                return NotFound();
            }
            return await _todoMVCBusinessServices.UpdateTodoItemAsync(todoItem);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        [HttpPut("UpdateTodoItemStatus/{id}")]
        public async Task<ActionResult<bool>> UpdateTodoItemStatus(long id, UpdateTodoItemStatusArgs args)
        {
            if (!await _todoMVCBusinessServices.ExistTodoItemAsync(id))
            {
                return NotFound();
            }

            try
            {
                return await _todoMVCBusinessServices.UpdateTodoItemAsync(id, args.TodoItemStatus);
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        [HttpPut("UpdateTodoItemContent/{id}")]
        public async Task<ActionResult<bool>> UpdateTodoItemContent(long id, UpdateTodoItemContentArgs args)
        {
            if (!await _todoMVCBusinessServices.ExistTodoItemAsync(id))
            {
                return NotFound();
            }
            return await _todoMVCBusinessServices.UpdateTodoItemAsync(id, args.Content);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult<TodoItemResult>> PostTodoItem(AddTodoItemArgs args)
        {
            var todoItem = await _todoMVCBusinessServices.AddTodoItemAsync(args.Content);
            return todoItem.ToTodoItemResult();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<ActionResult<bool>> DeleteTodoItem(long id)
        {
            return await _todoMVCBusinessServices.DeleteTodoItemAsync(id);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        [HttpDelete("DeleteTodoItems")]
        public async Task<ActionResult<bool>> DeleteTodoItems([FromBody] DeleteTodoItemsArgs args)
        {

            return await _todoMVCBusinessServices.DeleteTodoItemsAsync(args.Ids);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetTodoItemStatus")]
        public async Task<Dictionary<string, int>> GetTodoItemStatus()
        {
            var dict = EnumHelper.ConvertEnumToDictonary(typeof(TodoItemStatus));
            return await Task.FromResult(dict);
        }
    }
}
